package com.nchu.react.webflux.action;

import com.nchu.react.webflux.model.Student;
import com.nchu.react.webflux.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author fujianjian
 * @project react-learning
 * @date 2018/9/21 下午2:44
 */
//@RestController
//@RequestMapping("/student/")
public class StudentAction {


    @Autowired
    private StudentService studentService;


    @GetMapping("fetch/all")
    public Flux<Student> fetchAllStudent() {
        return Flux.fromIterable(this.studentService.fetchAllStudent());
    }

    @GetMapping("{id}")
    public Mono<Student> getOne(@PathVariable Integer id) {
        return Mono.justOrEmpty(this.studentService.getById(id))
                .switchIfEmpty(Mono.error(new Exception("资源不存在")));
    }

}
